package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *登录逻辑实现
 */
@Controller
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @ResponseBody
    @RequestMapping(value = "/index/login_in", method= RequestMethod.POST)
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String vericode = request.getParameter("vericode");
        String generatedCode = (String) request.getSession().getAttribute("verityCode");

        if (name.equals("bob") && password.equals("123") && vericode.equalsIgnoreCase(generatedCode)) {
            response.getWriter().write("登录成功.");
        } else {
            response.getWriter().write("登录失败.");
        }
    }

}
